package com.example.seven.mynews.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.example.seven.mynews.R;


public class WelcomeActivity extends AppCompatActivity {

    boolean isFirst = false;
    private static  final  int GOHOME = 100; //常量，看跳转到哪个页面
    private static  final  int GOGUIDE = 101;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==GOHOME){
                goHome();
            }else{
                goGuide();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        SharedPreferences sharedPreferences = getSharedPreferences("xy",MODE_PRIVATE);
        isFirst = sharedPreferences.getBoolean("isFirstIn",true);//判断本地存储的isFirst是不是true
        if (isFirst){
            //是第一次
            handler.sendEmptyMessageDelayed(GOGUIDE,1500);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("isFirstIn",false);
            editor.commit();//提交
        }else {
            handler.sendEmptyMessageDelayed(GOHOME,1500);
        }
    }

    private void  goHome(){
        Intent aIntent = new Intent(WelcomeActivity.this,MainActivity.class);
        startActivity(aIntent);
        finish();
    }
    private void  goGuide(){
        Intent aIntent = new Intent(WelcomeActivity.this,GuideActivity.class);
        startActivity(aIntent);
        finish();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_welcome, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
